#!/bin/bash
# *************************************
# 功能: harbor脚本模板
# 作者: 肖晨光
# 联系: 1819329667@qq.com
# 版本: v0.1
# 日期: 2023-07-31
# *************************************
# 混合功能的脚本

# 基本环境定制
compose_cmd='docker-compose'
compose_bin="/usr/bin/${compose_cmd}" 

harbor_name='harbor'
harbor_version='v2.5.0'
harbor_soft="harbor-offline-installer-"${harbor_version}".tgz"
harbor_site='https://github.com/goharbor/harbor/releases/download'
harbor_url="${harbor_site}/${harbor_version}/${harbor_soft}"


soft_dir='/data/softs'
server_dir='/data/server'
harbor_conf="${harbor_name}.yml"
harbor_pass='123456'
harbor_data_dir="${server_dir}/${harbor_name}/data"
harbor_service_file='harbor.service'
harbor_service_path="/lib/systemd/system/${harbor_service_file}"
harbor_compose_file='docker-compose.yml'

# 识别操作系统
status=$(grep -i ubuntu /etc/issue)
[ -n "${status}" ] && os_type="Ubuntu" || os_type="CentOS"
[ "${os_type}" == "Ubuntu" ] && cmd_type="apt" || cmd_type="yum"

# 自动获取主机名
host_name=$(awk '/register/{print $2}' etc/hosts)


# harbor依赖环境
compose-install(){
  # 部署harbor依赖的compose环境
	wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	[ -f "${compose_cmd}" ] && "${cmd_type}" -y install "${compose_cmd}"
	mkdir /data/{softs,server) -p && cd /data/softs
${server_dir}/${harbor_name}/${harbor_compose_file}
}

# harbor文件获取
get_harbor(){
  # 获取harbor软件文件
	if [ ! -f "${soft_dir}/${harbor_soft}" ]; then
		mkdir "${soft_dir}" -p && cd "${soft_dir}"
		wget "${harbor_url}"

	fi
  # 文件的解压
	[ -d  "${server_dir}/${harbor_name}" ] && rm -rf "${server_dir}/${harbor_name}"
	tar xf "${soft_dir}/${harbor_soft}" -C "${server_dir}"
}

# harbor环境初始化

config_harbor(){
  # harbor环境的配置文件定制
	cd "${server_dir}/${harbor_name}"
	mv "${harbor_conf}.tmpl" "${harbor_conf}"
	sed -i "/tname: /s#hostname: .*#hostname: $host_name#" "${harbor_conf}"
	sed -i 's/https/# https/' "${harbor_conf}"
	sed -i 's/port: 443/# port: 443/' "${harbor_conf}"
	sed -i 's/certificate:/# certificate:/' "${harbor_conf}"
	sed -i 's/private_key:/# private_key:/' "${harbor_conf}"
	sed -i "s#harbor_admin_password: .*#harbor_admin_password: ${harbor_pass}#" "${harbor_conf}"
	sed -i "s#data_volume: .*#data_volume: ${harbor_data_dir} " "${harbor_conf}"
  # harbor 环境初始化
	"${server_dir}/${harbor_name}/prepare"
	"${server_dir}/${harbor_name}/install.sh"
	sleep 5
	"${compose_bin}" ps
	sleep 3
	"${compose_bin}" down
}

# harbor服务定制
harbor_service(){
  # 定制harbor服务启动文件
	cat > "${harbor_service_path}" <<-eof
	[Unit]
	Description=Harbor
	After=docker.service systemd-networkd.service systemd-resolved.service
	Requires=docker.service
	Documentation=http://github.com/vmware/harbor
	[Service]
	Type=simple
	Restart=on-failure
	RestartSec=5
	#需要注意harbor的安装位置
	ExecStart=${compose_bin} --file ${server_dir}/${harbor_name}/${harbor_compose_file} up
	ExecStop=${compose_bin} --file ${server_dir}/${harbor_name}/${harbor_compose_file} down
	[Install]
	WantedBy=multi-user.target
	eof
  # 服务启动
	systemctl daemon-reload
	systemctl enable harbor
	systemctl start harbor

}

# harbor用户定制
harbor_user(){
  # 定制harbor管理员用户

}

# harbor镜像仓库
harbor_proj(){
  # 定制harbor镜像仓库


}

# harbor环境检查
harbor_check(){
  # 综合检查harbor环境

}

# 主函数
main(){
	compose_install
	get_harbor
	config_harbor
	harbor_service
}

# 主函数执行
main
